package org.liukai.servlet_ems.servlet;

import org.liukai.servlet_ems.dao.EmpDao;
import org.liukai.servlet_ems.dao.UserDao;
import org.liukai.servlet_ems.entity.po.Emp;
import org.liukai.servlet_ems.entity.po.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @project servlet_ems
 * @create MyServlet.java 
 * 
 * @author liukai
 * @date 2016/1/18 10:59
 * @detail 
 */
public class MyServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集 防止乱码
        request.setCharacterEncoding("utf-8");
        //得到uri
        String uri = request.getRequestURI();
        String[] uriString = uri.split("/");
        String action = uriString[uriString.length - 1];
        String type = uriString[uriString.length - 2];
        action = action.substring(0, action.lastIndexOf("."));
        System.out.println(type + "-----" + action);
        String contextPath = request.getContextPath();
        if("emp".equals(type)) {    //emp部分
            if("empList".equals(action)) {  //查所有
                request.setAttribute("empList", EmpDao.queryEmpList());
                //getRequestDispatcher 转发器
                request.getRequestDispatcher("/WEB-INF/emp/empList.jsp").forward(request, response);
                return;
            } else if("addEmpView".equals(action)) {    //跳转新增页面
                request.getRequestDispatcher("/WEB-INF/emp/addEmp.jsp").forward(request, response);
                return;
            } else if("addEmp".equals(action)) {    //新增emp
                Emp emp = new Emp();
                //request.getParameter得到的全是字符串 需要转换
                emp.setMarry(new Boolean(request.getParameter("marry")));
                emp.setName(request.getParameter("name"));
                emp.setAge(new Integer(request.getParameter("age")));
                emp.setSal(new Double(request.getParameter("sal")));
                EmpDao.addEmp(emp);
                //sendRedirect 重定向
                response.sendRedirect(contextPath + "/emp/empList.action");
                //response.sendRedirect后最好加上return
                return;
            } else if("deleteEmp".equals(action)) {
                Integer empId = new Integer(request.getParameter("empId"));
                EmpDao.deleteEmp(empId);
                response.sendRedirect(contextPath + "/emp/empList.action");
                return;
            } else if("updateEmpView".equals(action)) {
                Integer empId = new Integer(request.getParameter("empId"));
                request.setAttribute("emp", EmpDao.queryEmpById(empId));
                request.getRequestDispatcher("/WEB-INF/emp/updateEmp.jsp").forward(request, response);
                return;
            } else if("updateEmp".equals(action)) {
                Emp emp = new Emp();
                emp.setEmpId(new Integer(request.getParameter("empId")));
                emp.setMarry(new Boolean(request.getParameter("marry")));
                emp.setName(request.getParameter("name"));
                emp.setAge(new Integer(request.getParameter("age")));
                emp.setSal(new Double(request.getParameter("sal")));
                EmpDao.updateEmp(emp);
                response.sendRedirect(contextPath + "/emp/empList.action");
                return;
            }
        } else if("user".equals(type)) {    //user部分  目前只有登陆登出
            if("login".equals(action)) {
                String username = request.getParameter("username");
                String pwd = request.getParameter("pwd");
                User user = new User();
                user.setPwd(pwd);
                user.setUsername(username);
                User u = UserDao.queryUserByName(username);
                if(u == null) {
                    request.setAttribute("msg", "用户不存在");
                } else if(!pwd.equals(u.getPwd())) {
                    request.setAttribute("msg", "密码错误");
                } else {
                    //当前登录用户放入session
                    request.getSession().setAttribute("currentUser", u);
                    response.sendRedirect(contextPath + "/emp/empList.action");
                    return;
                }
                request.setAttribute("user", user);
                request.getRequestDispatcher("/WEB-INF/user/login.jsp").forward(request, response);
                return;
            } else if("logout".equals(action)) {    //登出
                //session失效
                request.getSession().invalidate();
                response.sendRedirect(contextPath + "/user/toLogin.action");
                return;
            } else if("toLogin".equals(action)) {   //跳转登录页
                request.getRequestDispatcher("/WEB-INF/user/login.jsp").forward(request, response);
                return;
            }
        } else if("error".equals(type)) {   //通用错误处理
            if("error".equals(action)) {
                request.getRequestDispatcher("/WEB-INF/error/error.jsp").forward(request, response);
            }
        }
        //上面所有的forward和redirect后加上return的原因是为了下面代码  错误处理
        request.getRequestDispatcher("/WEB-INF/error/error.jsp").forward(request, response);


    }
}
